<%@ page import="com.ld.businesscard_manage.service.UserService" %>
<%@ page import="com.ld.businesscard_manage.service.impl.UserServiceImpl" %>
<%@ page import="com.ld.businesscard_manage.bean.User" %>
<%@ page import="org.apache.commons.codec.digest.DigestUtils" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-02
  Time: 10:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加用户结果</title>
</head>
<body>
<%!
  private UserService userService = new UserServiceImpl();
%>
<%
  String userName = request.getParameter("username");
  String password = request.getParameter("password");
  String repassword = request.getParameter("repassword");
  if(userName == null || userName == "") response.sendRedirect("/home/addUser.jsp");
  else if(password == null || password == "") response.sendRedirect("/home/addUser.jsp");
  else if(repassword == null || repassword == "") response.sendRedirect("/home/addUser.jsp");
  else if(!repassword.equals(password)) {
    out.println("<h1>两次输入密码不一致</h1>");
  }else {
    User user = new User();
    password =  DigestUtils.md5Hex(request.getParameter("password"));
    user.setUsername(userName);
    user.setPassword(password);
    user.setStatus(0);
    User userByName = userService.getUserByName(userName);
    if (userByName != null) {
      out.println("该用户已经存在");
    } else {
      int i = userService.addUser(user);
      if (i == 1) {
        out.println("<h1>添加成功</h1>");
      } else {
        out.println("<h1>添加失败,请稍后重试</h1>");
      }
    }
  }
  out.println("<a href='/home/manageUser.jsp'>返回用户管理页</a>");
%>
</body>
</html>
